//
//  HomeViewCell.swift
//  swiftDemo
//
//  Created by ChuanQi on 2020/6/17.
//  Copyright © 2020 ChuanQi. All rights reserved.
//

import UIKit
import Kingfisher
class HomeViewCell: UITableViewCell {
    let imageView_W = 120.0//w:h = 4:3
    let imageView_H = 90.0
    let subView_interval:CGFloat = 10.0
    
    var leftImageView : UIImageView?
    var nameLabel     : UILabel?
    var subNameLabel  : UILabel?
    var timeLabel     : UILabel?
    var browseLabel   : UILabel?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
        
        self.createCellUI()
    }
    var model : HomeModel?{
        didSet {
            guard let model = model else {return}
            self.nameLabel?.text = model.title as String?;
            self.subNameLabel?.text = model.des as String?;
            self.timeLabel?.text = model.timer as String?;
            var constantString = "浏览量："
            constantString += (model.ll as String?)!;
            self.browseLabel?.text = "浏览量：(\((model.ll as String?)!))"
        }
    }
    
    func createCellUI(){
        
        leftImageView = UIImageView.init(frame : CGRect(x:15.0,y:5.0,width:110,height: 90))
        let path = Bundle.main.path(forResource: "angry_birds_cake", ofType: "jpg")
        let newImage = UIImage(contentsOfFile: path!)
        leftImageView?.image = newImage;
        leftImageView!.backgroundColor = UIColor.red;
        self.contentView.addSubview(leftImageView!)

        
        //name
        nameLabel = UILabel.init(frame: CGRect(x:130,y:subView_interval,width:SCREEN_WIDTH - Max_X(object:leftImageView!) - 2 * subView_interval,height:25.0))
        nameLabel?.textColor = UIColor.darkText
        nameLabel?.font = UIFont.systemFont(ofSize: 18)
        nameLabel?.text = "世界杯开幕";
        self.contentView.addSubview(nameLabel!)
        
        subNameLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:Max_Y(object: nameLabel!) + 5,width:W(object: nameLabel!),height:25))
        subNameLabel?.textColor = UIColor.darkGray
        subNameLabel?.font = UIFont.systemFont(ofSize: 15)
        subNameLabel?.text = "世界杯开幕";
        self.contentView.addSubview(subNameLabel!)
        
        timeLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:100-25,width:W(object: subNameLabel!) * 0.6,height:20))
        timeLabel?.textColor = UIColor.lightGray
        timeLabel?.font = UIFont.systemFont(ofSize: 13)
        timeLabel?.text = "2018-01-01 10:58"
        self.contentView.addSubview(timeLabel!)
        
        browseLabel = UILabel.init(frame: CGRect(x:Max_X(object: timeLabel!),y:Y(object: timeLabel!),width:W(object: subNameLabel!) * 0.4,height:20))
        browseLabel?.textAlignment = NSTextAlignment.right
        browseLabel?.textColor = UIColor.lightGray
        browseLabel?.font = UIFont.systemFont(ofSize: 13)
        browseLabel?.text = "浏览："+"50"
        self.contentView.addSubview(browseLabel!)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
}
